<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/01/15 07:27:09 $)
// $RCSfile: mobile_newticket.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("tickets_auditlogs");
require_once ("./modules/tickets/functions_ticketsui.php");
require_once ("./modules/tickets/functions_ticketcore.php");
require_once ("./modules/tickets/functions_ticketmain.php");
require_once ("./modules/tickets/functions_newticket.php");
require_once ("./includes/functions_customfields.php");
require_once ("./includes/functions_users.php");
require_once ("./includes/functions_mail.php");

// Check for variables
if (!empty($_POST["departmentid"]) && !empty($_POST["newtickettype"]))
{
	//Mahesh Slaria: Check for permissions.
	// Varun Shoor: Added t_restrictnewticket setting to toggle this
	$_assigns = getAssignedDepartments();
	if ($_SWIFT["staff"]["tnewticket"] == "0" || ($_SWIFT["settings"]["t_restrictnewticket"] == 1 && !in_array(intval($_POST["departmentid"]), $_assigns))) {
		// Dispatch Invalid XML ERROR
		$xml->addParentTag("mobile");

			$xml->addTag("successes", "0");
			$xml->addTag("errors", "1");

			$xml->addParentTag("error");
				$xml->addTag("ticketdisplayid", "-");
				$xml->addTag("type", "general");
				$xml->addTag("message", $_SWIFT["language"]["nopermission"]);
			$xml->endTag("error");

		$xml->endTag("mobile");

		$xml->echoXMLUTFWinapp();
		exit;
	}

	//-----------/
	if ($_SWIFT["staff"]["tnewticket"] != 0) {
		//Mahesh Slaria: Staff & user ticket creation
		if (trim($_POST["replycontents"]) == "" || trim($_POST["subject"]) == "")
		{
			$xml->addParentTag("mobile");

				$xml->addTag("successes", "0");
				$xml->addTag("errors", "1");

				$xml->addParentTag("error");
					$xml->addTag("ticketdisplayid", "-");
					$xml->addTag("type", "general");
					$xml->addTag("message", $_SWIFT["language"]["requiredfieldempty"]);
				$xml->endTag("error");

			$xml->endTag("mobile");

			$xml->echoXMLUTFWinapp();
			exit;
		} else if ($_POST["type"] == "email" && (empty($_POST["useremail"]) || !isValidEmail($_POST["useremail"]))) {
			$xml->addParentTag("mobile");

				$xml->addTag("successes", "0");
				$xml->addTag("errors", "1");

				$xml->addParentTag("error");
					$xml->addTag("ticketdisplayid", "-");
					$xml->addTag("type", "general");
					$xml->addTag("message", $_SWIFT["language"]["requiredfieldempty"]);
				$xml->endTag("error");

			$xml->endTag("mobile");

			$xml->echoXMLUTFWinapp();
			exit;
		} else {
			// Do we have an email queue set?
			if (!empty($_POST["emailqueueid"]))
			{
				$_emailqueue = $_SWIFT["queuecache"]["list"][$_POST["emailqueueid"]];
				$_tgroupid = $_emailqueue["tgroupid"];
			} else {
				$_emailqueue = array();
				$_tgroupid = 1;
			}

			// Lets see if we have an email set for this user?
			if (!empty($_POST["useremail"]))
			{
				$userid = getUserEmail($_POST["useremail"]);
				if (!$userid)
				{
					// No such user, insert a new one
					$usergroupid = $_SWIFT["tgroupcache"][$_tgroupid]["regusergroupid"];
					if (empty($usergroupid))
					{
						$usergroupid = 2;
					}
					$userid = insertUser(true, $_POST["useremail"], substr(buildHash(),0, 8), $usergroupid, LOGINAPI_DEFAULT, 0, $_POST["fullname"], 0, 0, false, 1, true);
				}
			}

			if (empty($_POST["fullname"]) && $_POST["type"] == "phone")
			{
				$fullname = $_SWIFT["staff"]["fullname"]; // Staff creating ticket as client, set full name to that of staff
			} else if (!empty($_POST["fullname"])) {
				$fullname = $_POST["fullname"];
			} else {
				$fullname = $_SWIFT["staff"]["fullname"]; // Set full name to staff
			}

			if ($_POST["type"] == "phone")
			{
				$posttype = POST_CLIENT;
			} else {
				$posttype = POST_STAFF;
			}

			if ($_POST["sendautoresponder"] == "1" && !empty($_POST["useremail"]))
			{
				$sendautoresponder = true;
			} else {
				$sendautoresponder = false;
			}

			$ticketiddata = createTicket($posttype, $_SWIFT["staff"]["staffid"], $userid, $fullname, $_POST["useremail"], $_POST["subject"], $_POST["replycontents"], 0, $_POST["priorityid"], $_POST["ticketstatusid"], $_POST["departmentid"], $_POST["emailqueueid"], $_SWIFT["language"]["charset"], "", $_tgroupid, false, false, $sendautoresponder, iif(empty($_POST["userphone"])&&$_POST["type"]=="phone", "-1", $_POST["userphone"]));

			$ticketid = $ticketiddata[0];
			$messageid = $ticketiddata[3];

			$_ticketobj =& new TicketMain($ticketid, false);
			$_ticketobj->messageid = $messageid;

			$mailObj = new SWIFT_Mail();
			$ccemaillist = $bccemaillist = array();
/*
			$cc = $_POST["opt_cc"];
			$bcc = $_POST["opt_bcc"];
			if (!empty($cc))
			{
				if (strstr($cc, ","))
				{
					$ccemaillist = explode(",", $cc);
				} else if (isValidEmail($cc)) {
					$ccemaillist = array($cc);
				}
				if ($_POST["ccaddrecipients"] == "1")
				{
					$_ticketobj->addRecipients($ccemaillist, RECIPIENT_CCUSER);
				}
			}

			if (!empty($bcc))
			{
				if (strstr($bcc, ","))
				{
					$bccemaillist = explode(",", $bcc);
				} else if (isValidEmail($bcc)) {
					$bccemaillist = array($bcc);
				}
				if ($_POST["bccaddrecipients"] == "1")
				{
					$_ticketobj->addRecipients($bccemaillist, RECIPIENT_BCCUSER);
				}
			}*/

			if ($_POST["sendcontents"] == "1" && !empty($_POST["useremail"]))
			{
				sendNewTicketEmail($_ticketobj, $mailObj, $_tgroupid, $ticketiddata[1], $_POST["useremail"], $_POST["subject"], $_POST["replycontents"], $_POST["emailqueueid"], $ccemaillist, $bccemaillist);
			}

/*			//Mahesh Slaria: Double attachment error....
			$_attachmentlist = array();
			$_ticketobj->processPostAttachments($ticketiddata[2], $_POST["opt_downloads"], $mailObj, $_attachmentlist);*/

			if (!empty($_POST["ownerstaffid"]))
			{
				$_ticketobj->assignTicket($_POST["ownerstaffid"]);
			}
			if (trim($_POST["notecontents"]) != "")
			{
				$_ticketobj->addTicketNote($_POST["notetype"], "0", $_POST["notecontents"]);
			}
/*			if (!empty($_POST["opt_due"]))
			{
				$_ticketobj->markAsDue(getCalendarDateline($_POST["opt_due"]));
			}*/

/*			//Mahesh: Timeworked track....
			if (!empty($_POST["opt_timeworked"]) && is_numeric($_POST["opt_timeworked"]))
			{
				$_ticketobj->addBillingEntry(DATENOW, $_POST["opt_timeworked"], $_POST["opt_timeworked"], $_SWIFT["staff"]["staffid"], "");
			}
			//--------------------*/

			if ($ticketid)
			{
				// Dispatch the result
				$xml->addParentTag("mobile");

					$xml->addTag("successes", $_successcount);
						$xml->addParentTag("success");
							$xml->addTag("ticketid", intval($ticketid));
							$xml->addTag("ticketdisplayid", $ticketiddata[0]);
							$xml->addTag("type", 'newticket');
							$xml->addTag("data", 'Success');
						$xml->endTag("success");

					$xml->addTag("errors", 0);

				$xml->endTag("mobile");

				$xml->echoXMLUTFWinapp();
				exit;
			} else {
				// Dispatch Invalid XML ERROR
				$xml->addParentTag("mobile");

					$xml->addTag("successes", "0");
					$xml->addTag("errors", "1");

					$xml->addParentTag("error");
						$xml->addTag("ticketdisplayid", "-");
						$xml->addTag("type", "general");
						$xml->addTag("message", $_SWIFT["language"]["invalidmobilexml"]);
					$xml->endTag("error");

				$xml->endTag("mobile");

				$xml->echoXMLUTFWinapp();
				exit;
			}
		}
	}

} else {
	// Dispatch Invalid XML ERROR
	$xml->addParentTag("mobile");

		$xml->addTag("successes", "0");
		$xml->addTag("errors", "1");

		$xml->addParentTag("error");
			$xml->addTag("ticketdisplayid", "-");
			$xml->addTag("type", "general");
			$xml->addTag("message", $_SWIFT["language"]["invalidmobilexml"]);
		$xml->endTag("error");

	$xml->endTag("mobile");

	$xml->echoXMLUTFWinapp();
}
?>